<template>
    <div class="input-item">
      <el-form :model="deviceform"  ref="deviceform" :rules="rules"  label-width="250px">
          <div class="parameType-wrap">
              <div class="parameType">
                <div class="button">
                  <!-- <span>终端代理配置</span> -->
                  <el-button style="float: right; padding:4px 8px;" type="primary" @click="operationClick('deviceform')">确定</el-button>
                </div>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理心跳间隔：" prop="agent_hand_shake_interval">
                    <el-input class="input-style" v-model.number="deviceform.agent_hand_shake_interval"><span slot="append">秒</span></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理心跳超时次数：" prop="lost_hand_shake_times">
                    <el-input class="input-style" v-model.number="deviceform.lost_hand_shake_times"><span slot="append">次</span></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理网络连接检测间隔：" prop="check_agent_net_status_interval">
                    <el-input class="input-style" v-model.number="deviceform.check_agent_net_status_interval"><span slot="append">秒</span></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理定时更新参数间隔：" prop="refresh_agent_param_interval">
                    <el-input class="input-style" v-model.number="deviceform.refresh_agent_param_interval"><span slot="append">秒</span></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理离线日志上送间隔：" prop="agent_report_offline_interval">
                    <el-input class="input-style" v-model.number="deviceform.agent_report_offline_interval"><span slot="append">秒</span></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理离线日志保存尺寸：" prop="agent_offline_max_file_size">
                    <el-input class="input-style" v-model.number="deviceform.agent_offline_max_file_size"><span slot="append">M</span></el-input>
                    <span class="tips-style">（0代表不保存）</span>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :lg="24">
                  <el-form-item label="终端代理自动升级检测间隔：" prop="agent_auto_upgrade_interval">
                    <el-input class="input-style" v-model.number="deviceform.agent_auto_upgrade_interval"><span slot="append">秒</span></el-input>
                    <span class="tips-style">（0表示不升级）</span>
                  </el-form-item>
                </el-col>
              </div>
          </div>
      </el-form>
    </div>
</template>

  <script>
  import { connrequest } from '@/api/firstPage'
  import { validNum } from '@/utils/validate'
  export default {
      data() {
          return {
              deviceform:{
                agent_hand_shake_interval:'0',
                lost_hand_shake_times:'0',
                check_agent_net_status_interval:'0',
                refresh_agent_param_interval:'0',
                agent_report_offline_interval:'0',
                agent_offline_max_file_size:'0',
                agent_auto_upgrade_interval:'0'
              },
              oldData:[],
              rules:{
                agent_hand_shake_interval:[{required:true,validator:validNum,trigger:'blur'}],
                lost_hand_shake_times:[{required:true,validator:validNum,trigger:'blur'}],
                check_agent_net_status_interval:[{required:true,validator:validNum,trigger:'blur'}],
                refresh_agent_param_interval:[{required:true,validator:validNum,trigger:'blur'}],
                agent_report_offline_interval:[{required:true,validator:validNum,trigger:'blur'}],
                agent_offline_max_file_size:[{required:true,validator:validNum,trigger:'blur'}],
                agent_auto_upgrade_interval:[{required:true,validator:validNum,trigger:'blur'}],
              }
          }
      },
      created(){
        this.getData()
        // console.log();

      },
      methods:{
        //
        async getData(){
          // const data = {}
          const res = await connrequest('GetParamThreshhold')
          // console.log(res.Data);
          this.oldData = res.Data;
          res.Data = res.Data.map(item => {
            for (const i in this.deviceform) {
              if (item.Name === i) {
                this.deviceform[i] = item.Value
              }
            }

          })
        },
        async operationClick(formName){
          this.$refs[formName].validate(async(valid) => {
            if (valid) {
              this.oldData = this.oldData.map(item => {
              for (const key in this.deviceform) {
                if(item.Name === key){
                  if (typeof this.deviceform[key] != 'string') {
                    item.Value = JSON.stringify(this.deviceform[key])
                  }else{
                    item.Value = this.deviceform[key]
                  }
                }
                }
                return item
              })
              const data = {
                Data:this.oldData
              }
              const res = await connrequest('SetParamThreshhold',data)
              this.$message.success('修改成功')
              this.getData();
            }
          })
        }
      }
  }
  </script>

  <style lang="scss" scoped>
  /* @import url(); 引入css类 */
    .parameType-wrap{
      display:flex;
      flex-wrap: wrap;
    }
    ::v-deep.input-item{
      // margin-top: 10px;
      .parameType{
        border: 1px solid #eceff5;
        border-radius: 3px;
        width: 800px;
      //   padding: 10px;
        margin: 0px;
        box-shadow: 0 0 10px #eceff5;
        .button{

          border-bottom: 1px solid #eceff5;
          padding-bottom : 32px;
        }
      }
      .el-form-item{
        margin-bottom: 18px;
      }
      .el-input-group__append, .el-input-group__prepend{
        width: 60px;
        padding: 0;
        text-align: center;
      }
    }
    .button{
      margin-bottom: 20px;
    }
    ::v-deep.el-form-item{
      label{
        font-weight: 500;
      }
    }
    .input-style{
        width: 70% !important;
    }

  </style>
